﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SudokuSquare : System.Web.UI.UserControl
{
    private string row;
    private string column;
    private string regionRow;
    private string regionColumn;

    public string RegionRow
    {
        get
        {
            return this.regionRow;
        }
        set
        {
            this.regionRow = value;
        }
    }

    public string RegionColumn
    {
        get
        {
            return this.regionColumn;
        }
        set
        {
            this.regionColumn = value;
        }
    }

    public string Row
    {
        get
        {
            return this.row;
        }
        set
        {
            this.row = value;
        }
    }

    public string Column
    {
        get
        {
            return this.column;
        }
        set
        {
            this.column = value;
        }
    }

    private int PuzzleValue
    {
        get
        {
            return ((SudokuState.SudokuState)Session["puzzle"]).Get(
                this.OverallRow, this.OverallColumn);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            System.Diagnostics.Debug.WriteLine(
                String.Format("- - Rendering sqaure {0},{1}", OverallRow + 1, OverallColumn + 1));
            this.square.Attributes["data-row"] = this.Row.ToString();
            this.square.Attributes["data-column"] = this.Column.ToString();
        }
        int value = this.PuzzleValue;
        System.Diagnostics.Debug.WriteLine(
            String.Format("- - Puzzle value = {0}", value));
        if (value != 0)
        {
            this.squareInput.Text = value.ToString();
        }
    }

    private Boolean valueInput = false;

    private int OverallRow
    {
        get
        {
            return (3 * int.Parse(this.RegionRow)) + int.Parse(this.Row);
        }
    }

    private int OverallColumn
    {
        get
        {
            return (3 * int.Parse(this.RegionColumn)) + int.Parse(this.Column);
        }
    }
    protected void squareInput_TextChanged(object sender, EventArgs e)
    {
        SudokuState.SudokuState puzzle = (SudokuState.SudokuState)Session["puzzle"];
        TextBox box = (TextBox)sender;
        int oldValue = puzzle.Get(OverallRow, OverallColumn);
        int newValue = 0;
        System.Diagnostics.Debug.WriteLine(String.Format("squareInput changed {0}", box.Text));
        if (box.Text != null && !box.Text.Trim().Equals(""))
        {
            try
            {
                newValue = int.Parse(box.Text);
            }
            catch (Exception)
            {
                newValue = 0;
                box.Text = "";
            }
        }
        puzzle.Set(OverallRow, OverallColumn, newValue);
        String message = String.Format("Updated value: {0},{1} = {2} -> {3}",
            OverallRow, OverallColumn, oldValue, puzzle.Get(OverallRow, OverallColumn));
        System.Diagnostics.Debug.WriteLine(message);
        valueInput = true;
    }
}